addEventHandler("onPlayerJoin", getRootElement(), 
	function()
		local playerAccount = getPlayerAccount(source)
		
		if isGuestAccount(playerAccount) == true then
			triggerClientEvent(player, "guiRegister", getRootElement())
		else
			triggerClientEvent(source, "correctLogin", getRootElement())
			print("correctlogin")
		end

	end
)

addEvent("tryLogin", true)
addEventHandler("tryLogin", getRootElement(),
	function(player, user, password)
		local loginAccount = getAccount(user)
		
		if loginAccount == false or nil then
			triggerClientEvent(player, "identError", getRootElement(), 1)
			return
		end
		
		local playerAccount = logIn(player, loginAccount, password)
		
		if playerAccount == false then
			triggerClientEvent(player, "identError", getRootElement(), 0)
			return
		end		
	end
)

local root = getRootElement()
 
addEventHandler("onPlayerLogin", getRootElement(),
  function()
		local account = getPlayerAccount ( source )
		local account_name = getAccountName( account)
		
		local handle = dbQuery( conexion_DBpersonajes, "SELECT * FROM personajes WHERE account_name=?", account_name )
		local result, numrows, errmsg = dbPoll ( handle, -1 )
		
		if numrows > 0 then
		--Tiene un personaje asociado a la cuenta: spawn con ese personaje
			setElementPosition( source, 3,5,8)
			setCameraTarget ( source )
			toggleAllControls ( source, true )
			setElementFrozen ( source, false )
			triggerClientEvent(source, "correctLogin2", getRootElement())
		else
			triggerClientEvent(source, "correctLogin", getRootElement())

		end
  end
)

addEvent("registerGUIReady", true)
addEventHandler("registerGUIReady", getRootElement(),
	function(player)
		local playerAccount = getPlayerAccount(player)

		if isGuestAccount(playerAccount) then
			triggerClientEvent(player, "guiRegister", getRootElement())
		else
			triggerClientEvent(player, "correctLogin", getRootElement())
		end
	end
)

addEvent("tryRegister", true)
addEventHandler("tryRegister", getRootElement(),
	function(player, user, password)
		local newAccount = addAccount ( user, password )
		if newAccount == false then
			triggerClientEvent(player, "identError", getRootElement(), 2)
			return
		end
		
		logIn ( player, newAccount, password )
	end
)

addEvent("tryNewPlayer", true)
addEventHandler("tryNewPlayer", getRootElement(),
	function(player, nombre, apellido)
		local account = getPlayerAccount ( player )
		local account_name = getAccountName( account)
		
		local handle = dbQuery( conexion_DBpersonajes, "SELECT * FROM personajes WHERE nombre=? AND apellido=?",nombre,apellido )
		local result, numrows, errmsg = dbPoll ( handle, -1 )
		
		print (numrows)
		if numrows == 0 then
		--No existe un personaje con el nombre y apellido ingresados: Crear personaje en la DB
			dbExec ( conexion_DBpersonajes, "INSERT INTO personajes (account_name, nombre, apellido) VALUES (?,?,?)", account_name, nombre, apellido)
			triggerClientEvent( player, "onNewCharCreate", getRootElement() )
		else
		--Error, el nombre y apellidos ya existen.
			triggerClientEvent( player, "newPlayerCreationError", getRootElement(), 0)
		end
		
		
		
	end
)